package pl.enigmatic.reflection;

import java.lang.reflect.Field;


public class PublicPropertySetter implements Setter {

	private final Field field;
	private final PropertyPath path;

	public PublicPropertySetter(final Class<?> cls, final String property) throws SecurityException, NoSuchFieldException {
		field = cls.getField(property);
		path = new PropertyPath(property);
	}

	@Override
	public boolean set(final Object target, final Object value) {
		try {
			field.set(target, value);
			return true;
		} catch (final Exception e) {
			return false;
		}
	}

	@Override
	public Class<?> getType() {
		return field.getType();
	}

	@Override
	public PropertyPath getPath() {
		return path;
	}

}
